package com.cn.jx.manager.service.query.Impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.cn.jx.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.common.util.page.PageObj;
import com.cn.jx.manager.dao.ResourceRepository;
import com.cn.jx.manager.po.Resource;
import com.cn.jx.manager.service.query.IResourceServiceQry;

import com.jx.enumType.ApplyEnum;
import com.jx.enumType.ResourceTypeEnum;
import com.jx.jpaQuery.QueryCondition;
import com.jx.model.MenuEntry;
import com.jx.util.ArrayUtil;
import com.jx.util.Constant;
import com.jx.util.DateUtil;
import com.jx.util.MapObject;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月1日
 * <p>功能描述：
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Service("resourceServiceQry") 
@Transactional
public class ResourceServiceImplQry extends BaseService implements IResourceServiceQry {

	/**定义{@link ResourceRepository resourceDao <tt>资源Dao层接口定义</tt>}*/
	@Autowired
	private ResourceRepository resourceDao;
	
	/* 分页查询资源 */
	@Override
	@Transactional(readOnly = true)  
	public PageObj<MapObject> findResourceByPageAndParams(QueryCondition cond)  throws Exception {
		Specification<Resource> speci=getSpecification(cond);
		Pageable pageable =getPageables(cond);
		Page<Resource> res=resourceDao.findAll(speci,pageable);
		List<Resource> list=res.getContent();
		List<MapObject> listContent=new ArrayList<MapObject>();
		if(ArrayUtil.listNotEmpty(list)){
			list.forEach(rs->{
				MapObject mp=MapObject.getInstance();
				mp.put("text", rs.getText());
				mp.put("code",rs.getCode());
				mp.put("url", rs.getUrl());
				mp.put("iconCls", rs.getIconCls());
				mp.put("type", ResourceTypeEnum.getName(rs.getType()));
				mp.put("apply", ApplyEnum.getName(rs.getApply()));
				mp.put("sort",rs.getSort());
				mp.put("createTime", DateUtil.getDateFmt(rs.getCreateTime()));
				mp.put("id", rs.getId());
				listContent.add(mp);
			});
		}
		PageObj<MapObject> pageRes=new PageObj<MapObject>(res,listContent);
		return pageRes;
	}

	/*  根据主键获取对象 */
	@Override
	@Transactional(readOnly = true)  
//	@RedisCacheSave(key="resourceService",value="'getResourceById='.concat(#id)",rTime=RedisTime.SIX_HOURS,cls=Resource.class)
	public Resource getResourceById(String id)  {
		return findEntityByPkid(id, Resource.class);
	}
	
	/* 资源管理列表 */
//	@RedisCacheSave(key="resourceService_menu",value="'findResourceTreeMgr'",rTime=RedisTime.SIX_HOURS,cls=MenuEntry.class)
	@Override
	@Transactional(readOnly = true)  
	public List<MenuEntry> findResourceTreeMgr(String parent){
		String parentroot=Utility.isBlank(parent)?Constant._ROOT:parent;
		List<MenuEntry> result=new ArrayList<MenuEntry>();
		List<Resource> rootlist=resourceDao.findAllResourceByTypeAndParent(parentroot);//资源管理功能树
		if(rootlist!=null && rootlist.size()>0){
			for(Resource psour:rootlist){
				MenuEntry mroot=new MenuEntry();
				mroot.setText(psour.getText());
				mroot.setUrl(Utility.trimStr(psour.getUrl()));
				mroot.setIconCls(Utility.trimStr(psour.getIconCls()));
				mroot.setCode(psour.getCode());
				mroot.setId(psour.getId());
				mroot.setType(psour.getType());
				List<MenuEntry> childlist=findResourceTreeMgr(psour.getId());
				mroot.setChildren(childlist);
				result.add(mroot);
			}
		}
		if(Utility.isBlank(parent)){//空表示想查所有
			MenuEntry mroot=new MenuEntry();
			mroot.setText("根菜单");
			mroot.setIconCls("");
			mroot.setCode(Constant._ROOT);
			mroot.setId(Constant._ROOT);
			mroot.setChildren(result);
			List<MenuEntry> praresult=new ArrayList<MenuEntry>();
			praresult.add(mroot);
			return praresult;
			
		}
		return result;
	}

	/* 所有菜单里面用 */
//	@RedisCacheSave(key="resourceService_menu",value="'findResourceTree_apply='.concat(#apply).concat('_parent=').concat(#parent).concat('_permissions=').concat(#permissions)",rTime=RedisTime.SIX_HOURS,cls=MenuEntry.class)
	@Override
	@Transactional(readOnly = true)  
	public List<MenuEntry> findResourceTree(ApplyEnum apply,String parent,List<String> permissions) {
		String parentroot=Utility.isBlank(parent)?Constant._ROOT:parent;
		List<MenuEntry> result=new ArrayList<MenuEntry>();
		List<Resource> rootlist=resourceDao.findAllResourceByPermissions(apply.getCode(),parentroot,permissions);//左侧菜单
		if(rootlist!=null && rootlist.size()>0){
			for(Resource psour:rootlist){
				MenuEntry mroot=new MenuEntry();
				mroot.setText(psour.getText());
				mroot.setUrl(Utility.trimStr(psour.getUrl()));
				mroot.setIconCls(Utility.trimStr(psour.getIconCls()));
				mroot.setCode(psour.getCode());
				mroot.setId(psour.getId());
				mroot.setType(psour.getType());
				List<MenuEntry> childlist=findResourceTree(apply,psour.getId(),permissions);
				mroot.setChildren(childlist);
				result.add(mroot);
			}
		}
		return result;
	}
	
	/* 根据父节点查询子项目数量 查询此节点下面有没有子项目 */
	@Override
	@Transactional(readOnly = true)  
	public Integer findResourceCountByParent(List<String> ids) throws Exception {
		for(String id : ids){
			Integer ct=resourceDao.findCountResourceByParent(id);
			if(ct!=null && ct>0){
				return ct;
			}
		}
		return 0;
	}
	
	/*角色分配资源权限控件资源列表*/
	@Override
	@Transactional(readOnly = true) 
//	@RedisCacheSave(key="resourceService_menu",value="'findResourceRoleTree='.concat(#selectedRes)",rTime=RedisTime.SIX_HOURS,cls=MenuEntry.class)
	public List<MenuEntry> findResourceRoleTree(String parent, Set<String> selectedRes) {
		String parentroot=Utility.isBlank(parent)?Constant._ROOT:parent;
		List<MenuEntry> result=new ArrayList<MenuEntry>();
		//获取所有子资源有效的 因为这里是管理资源 角色分配 需要查询所有而不是根据权限查询
		List<Resource> rootlist=resourceDao.findAllResourceByParent(parentroot);
		if(rootlist!=null && rootlist.size()>0){
			for(Resource psour:rootlist){
				MenuEntry mroot=new MenuEntry();
				mroot.setText(psour.getText());
				mroot.setUrl(Utility.trimStr(psour.getUrl()));
				mroot.setIconCls(Utility.trimStr(psour.getIconCls()));
				mroot.setCode(psour.getCode());
				mroot.setId(psour.getId());
				mroot.setType(psour.getType());
				List<MenuEntry> childlist=findResourceRoleTree(psour.getId(),selectedRes);
				if(childlist.size()==0){//叶子节点选中
					mroot.setChecked(selectedRes.contains(psour.getId()));
				}
				mroot.setChildren(childlist);
				result.add(mroot);
			}
		}
		if(Utility.isBlank(parent)){//空表示想查所有
			MenuEntry mroot=new MenuEntry();
			mroot.setText(Constant._ROOT_MENU);
			mroot.setIconCls(Constant._EMPTY);
			mroot.setCode(Constant._ROOT);
			mroot.setId(Constant._ROOT);
			mroot.setChildren(result);
			List<MenuEntry> praresult=new ArrayList<MenuEntry>();
			praresult.add(mroot);
			return praresult;
			
		}
		return result;
	}

	@Override
	@Transactional(readOnly = true) 
	public List<Resource> findAllResource() {
		return resourceDao.findAllResource();
	}

	@Override
	@Transactional(readOnly = true) 
	public List<Resource> findAllResourceByIds(List<String> ids) {
		return resourceDao.findResourceByIds(ids);
	}

	@Override
	@Transactional(readOnly = true) 
	public List<String> findAllResourceCode() {
		return resourceDao.findAllResourceCode();
	}

	@Override
	@Transactional(readOnly = true) 
	public List<Resource> findAllResourceByParent(String parentId) {
		return resourceDao.findAllResourceByParent(parentId);
	}

	@Override
	@Transactional(readOnly = true) 
	public List<Resource> findAllResourceByParentCode(String parentCode) {
		return resourceDao.findAllResourceByParentCode(parentCode);
	}

}
